<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 7/22/14
 * Time: 3:44 PM
 */

namespace User\Mapper;

use Zend\Crypt\Password\PasswordInterface as ZendCryptPassword;
use Zend\Stdlib\Hydrator\ClassMethods;
use Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator;
use User\Entity\UserInterface as UserEntity;

class UserHydrator extends ClassMethods implements HydratorInterface
{
    /**
     * @var ZendCryptPassword
     */
    private $cryptoService;

    /**
     * @param ZendCryptPassword $cryptoService
     * @param bool|array        $underscoreSeparatedKeys
     */
    public function __construct(ZendCryptPassword $cryptoService,$underscoreSeparatedKeys = true)
    {
        parent::__construct($underscoreSeparatedKeys);
        $this->cryptoService = $cryptoService;
    }

    /**
     * @return ZendCryptPassword
     */
    public function getCryptoService()
    {
        return $this->cryptoService;
    }


    /**
     * Remap an array key
     *
     * @param  string $keyFrom
     * @param  string $keyTo
     * @param  array  $array
     * @return array
     */
    public function mapField($keyFrom , $keyTo , array $array)
    {
        if (isset($array[$keyFrom])) {
            $array[$keyTo] = $array[$keyFrom];
        }
        unset($array[$keyFrom]);
        return $array;
    }
    /**
     * Ensure $object is an UserEntityInterface
     *
     * @param  mixed $object
     * @throws Exception\InvalidArgumentException
     */


    protected function guardUserObject($object)
    {
        if(!$object instanceof UserEntity)
        {
            throw new Exception\InvalidArgumentException(
                '$object must be an instance of User\Entity\UserInterface'
            );
        }
    }

    public function hydrate(array $data , $object)
    {
        $this->guardUserObject($object);
       // $data = $this->mapField('roles', 'role_id', $data);
        if(isset($data['role_id']) && isset($data['role_name']) && (int)$data['role_id'] >= 0)
        {
            $roles = new \RoleUser\Entity\Role();
            $roles->setId($data['role_id']);
            $roles->setRoleId($data['role_name']);
            $data['roles'] = $roles;
            $data = $this->mapField('roles', 'role_id', $data);
        }

        if(isset($data['image_id']) && isset($data['image_path']) && (int)$data['image_id'] >= 0)
        {
            $avatar = new \UploadFile\Entity\Image();
            $avatar->setImageId($data['image_id']);
            $avatar->setImagePath($data['image_path']);
            $data['image'] = $avatar;
            $data = $this->mapField('image', 'avatar', $data);
        }

        //var_dump($data);
        //var_dump($object);
        return parent::hydrate($data, $object);
    }

    /**
     * Extract values from an object
     *
     * @param  UserEntityInterface $object
     * @return array
     * @throws Exception\InvalidArgumentException
     */
    public function extract($object)
    {
        $this->guardUserObject($object);
        $data = parent::extract($object);
        //return $this->mapField('id', 'user_id', $data);
        return $data;
    }

} 